package com.ruix.app.network;

import android.content.Context;
import android.net.ConnectivityManager;
import android.net.NetworkInfo;
import android.text.TextUtils;

import com.ruix.app.network.cookie.CookiesManager;
import com.ruix.app.util.DeviceUtil;
import com.ruix.app.util.LogUitl;


import java.util.concurrent.TimeUnit;

import okhttp3.Headers;
import okhttp3.OkHttpClient;

/**
 * Created by hadexs on 2017/4/11.
 */

public class RequestManager {


    public static int RequstTimeout = 15;// 公共Headers
    public static String deviceID = "RUIX"+System.currentTimeMillis();

    public static Headers commonHeaders;// 公共Headers
    public static OkHttpClient okhttpClient;// 普通client
    private static CookiesManager cookieJar;

    // app：code、name
    public static String APP_VERSION_CODE;// app－version－code
    public static String APP_VERSION_NAME;// app－version－name


    private static Context context;
    public static boolean isNetOk;// 网络是否ok

    public static void init(Context appContext, String appVersionCode, String appVersionName) {
        initBaseData(appContext, appVersionCode, appVersionName);
        initOkhttpClient();//
    }


    protected static void initBaseData(Context appContext, String appVersionCode, String appVersionName) {
        context = appContext;
        APP_VERSION_CODE = appVersionCode;
        APP_VERSION_NAME = appVersionName;
        String deviceId =  DeviceUtil.id(appContext);
        if (!TextUtils.isEmpty(deviceId)){
            deviceID = deviceId;
        }
        checkNetStatus();// 检查网络状态
//        initSslEnvironment();// 初始化SSL环境
    }


    // 检查网络状态
    public static void checkNetStatus() {
        ConnectivityManager cm = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);
        NetworkInfo info = cm.getActiveNetworkInfo();
        if (info != null) {
            if (info.isAvailable()) {
                isNetOk = true;// 网络OK
            } else {
                isNetOk = false;// 网络不通
            }
        } else {
            isNetOk = false;// 网络不通
        }
    }


    public RequestManager() {
    }

    private static void initOkhttpClient() {
        initOkhttpHeaders();// 初始化Headers
        // 普通client
        okhttpClient = newClientBuilder().build();
//        okhttpClient.
//        okhttpShamClient = newClientBuilder()//
//                .sslSocketFactory(sslContext.getSocketFactory(), trustManager)//
//                .hostnameVerifier(noVerify)//
//                .build();
    }

    // 初始化Headers
    private static void initOkhttpHeaders() {
        commonHeaders = new Headers.Builder()
                .add("platform", "android")
                .add("device-id", deviceID)
                .add("version", "1")
                .build();
    }

    // 新建一个新的clientBuilder
    private static OkHttpClient.Builder newClientBuilder() {
         cookieJar = new CookiesManager(context);
        return new OkHttpClient.Builder()
                .readTimeout(RequstTimeout, TimeUnit.SECONDS)
                .connectTimeout(RequstTimeout, TimeUnit.SECONDS)
                .cookieJar(cookieJar);
    }

    public static void removeCookie(){
        if (cookieJar!= null) {
            cookieJar.logout();
        }
    }


}
